package client

import (
	"context"
	"fmt"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	monproto "gollect/proto" // Adjust import path as needed
)

// MonClient handles gRPC connections to monitored clients.
type MonClient struct {
	conn   *grpc.ClientConn
	client monproto.MonitoringServiceClient // Assuming service name is MonitoringService
}

// NewMonClient creates a new MonClient instance and connects to the given address.
func NewMonClient(address string) (*MonClient, error) {
	conn, err := grpc.Dial(address, 
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithBlock(),
		grpc.WithTimeout(5*time.Second))
	if err != nil {
		return nil, fmt.Errorf("failed to connect: %w", err)
	}
	client := monproto.NewMonitoringServiceClient(conn)
	return &MonClient{conn: conn, client: client}, nil
}

// Close closes the gRPC connection.
func (mc *MonClient) Close() error {
	return mc.conn.Close()
}

// FetchMonitoringData fetches monitoring data from the client.
func (mc *MonClient) FetchMonitoringData(ctx context.Context, req *monproto.MonitoringRequest) (*monproto.MonitoringEnvelope, error) {
	return mc.client.GetMonitoringData(ctx, req)
}

// TODO: Add methods to fetch monitoring data from clients 